﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace Golos.Users.UserCache
{
    [Serializable]
    public class UserInfo
    {
        public Guid Id { get; private set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public Dictionary<UserIdType, string> UserIdentification { get; private set; }
        public Dictionary<UserIdType, bool> SaveActivityToSocialWeb { get; private set; }

        public UserInfo(Guid id, string name, string email, UserIdType userIdType, string identificationId)
        {
            Id = id;
            Name = name;
            Email = email;
            UserIdentification = new Dictionary<UserIdType, string> { { userIdType, identificationId } };
            SaveActivityToSocialWeb = new Dictionary<UserIdType, bool> {{userIdType, false}};
        }

        public UserInfo(Guid id, string name, string email)
        {
            Id = id;
            Name = name;
            Email = email;
            UserIdentification = new Dictionary<UserIdType, string>();
            SaveActivityToSocialWeb = new Dictionary<UserIdType, bool>();
        }

        public void AttachUserIdentification(UserIdType userIdType, string identificationId)
        {
            if (!UserIdentification.ContainsKey(userIdType))
                UserIdentification.Add(userIdType, identificationId);
            else
                UserIdentification[userIdType] = identificationId;
            if(!SaveActivityToSocialWeb.ContainsKey(userIdType))
                SaveActivityToSocialWeb.Add(userIdType, false);
        }

        public void AttachUserIdentification(UserIdType userIdType, string identificationId, bool saveActivityToSocialWeb)
        {
            if (!UserIdentification.ContainsKey(userIdType))
                UserIdentification.Add(userIdType, identificationId);
            else
                UserIdentification[userIdType] = identificationId;
            if (!SaveActivityToSocialWeb.ContainsKey(userIdType))
                SaveActivityToSocialWeb.Add(userIdType, saveActivityToSocialWeb);
            else
                SaveActivityToSocialWeb[userIdType] = saveActivityToSocialWeb;
        }

        public byte[] Serialize()
        {
            MemoryStream stream = new MemoryStream();
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, this);
            stream.Close();
            return stream.ToArray();
        }

        public static UserInfo Deserialize(Stream stream)
        {
            BinaryFormatter bFormatter = new BinaryFormatter();
            UserInfo userInfo = (UserInfo)bFormatter.Deserialize(stream);
            stream.Close();
            return userInfo;
        }

        
    }
}
